package cn.tx;

import cn.Factory.*;
import cn.shape.Shape;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class DrawListener extends MouseAdapter {
    //定义坐标,起点坐标，终点坐标
    private int x1,y1,x2,y2;
    //定义工具栏面板的引入
    private ToolsPanel tp;
    //创建画笔
    private Graphics2D g;
    //把画布接受过来
    private DrawPanel dp;
    //定义工具属性
    private String tool;
   // private Shape shape;
   private Shape shape;
    /**
     * 初始化属性
     * @param tp
     * @param dp
     */
    public DrawListener(ToolsPanel tp, DrawPanel dp) {
         g = (Graphics2D) dp.panel.getGraphics();
        this.tp = tp;
        this.dp = dp;
       tool ="pen";
        System.out.println(tool);
    }
    //ShapeFactory shapeFactory = new LineShapeFactory();
    void createNewGraphics(){
        switch (tool){
            case "line": { //直线
                ShapeFactory shapeFactory = new LineShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                System.out.println("line");
                break;
            }
            case "point": {//点
                ShapeFactory shapeFactory = new PointShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                System.out.println("point");
                break;
            }
            case "pen": {//钢笔
                ShapeFactory shapeFactory = new PenShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                System.out.println("pen");
                break;
            }
            case "rubber": {//橡皮
                shape = new RubberShapeFactory().CreatHollowShape();
                break;
            }
            case "HollowRec": {//空心矩形
                ShapeFactory shapeFactory = new RecShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                break;
            }
            case "HollowTri": {//空心三角形
                ShapeFactory shapeFactory = new TriShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                break;
            }
            case "HollowCircle": {//空心圆形
                ShapeFactory shapeFactory = new CircleShapeFactory();
                shape = shapeFactory.CreatHollowShape();
                break;
            }
            case "SolidCircle": {//实心圆形
                ShapeFactory shapeFactory = new CircleShapeFactory();
                shape = shapeFactory.CreatSolidShape();
                break;
            }
            case "SolidRec": {//实心矩形
                ShapeFactory shapeFactory = new RecShapeFactory();
                shape = shapeFactory.CreatSolidShape();
                break;
            }
            case "SolidTri": {//实心三角形
                ShapeFactory shapeFactory = new TriShapeFactory();
                shape = shapeFactory.CreatSolidShape();
                break;
            }
            default:
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        //获得工具
        tool = tp.tool;
        if(tool == null)
            tool = "pen";
        createNewGraphics();
        //获得起点坐标
        shape.x1 = e.getX();
        shape.y1 = e.getY();
        System.out.println("起点坐标: "+shape.x1+"  "+shape.y1);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        //获得鼠标终点坐标
        shape.x2 = e.getX();
        shape.y2 = e.getY();
        //默认笔的粗细
        g.setStroke(new BasicStroke(3));
        g.setColor(Color.black);
        shape.draw(g);

     /*   //实心矩形
        if("SolidRec".equals(tool)){
            g.fillRect(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
        }

        //实心圆形
        if("SolidCircle".equals(tool)){
            g.fillOval(Math.min(x1,x2),Math.min(y1,y2),Math.abs(x2-x1),Math.abs(y2-y1));
        }

        //实心三角形
        if("SolidTri".equals(tool)){
            g.setStroke(new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_BEVEL));
            int[] x = { (x1 + x2) / 2, x1, x2 };
            int[] y = { y1, y2, y2 };
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            g.fillPolygon(x, y, 3);
        }*/

        System.out.println("终点坐标: "+shape.x2+"  "+shape.y2);


    }

    @Override
    public void mouseDragged(MouseEvent e) {
        //获得鼠标的终点坐标
        shape.x2 = e.getX();
        shape.y2 = e.getY();
        //画直线
        if("pen".equals(tool)){
            shape.draw(g);
        }

        if("rubber".equals(tool)){
            shape.draw(g);
        }
    }
}
